package com.cm.rpa.config;

import com.cm.kits.YamlKits;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.io.File;
import java.net.URISyntaxException;

/**
 * 机器人配置对象
 *
 * @author LWang 2023.01.28
 * @since 1.0.0
 */
@Data
@ToString
@NoArgsConstructor
public class RpaConfig {
    private Selenium selenium;
    private Browser browser;
    private Rpa rpa;
    private Double scale;
    /**
     * 新增参数
     */
    private boolean useSeleniumDrag;
    private boolean networkProxy;
    private boolean remoteDebugging;
    private int remoteDebuggingPort;
    private String remoteDebuggingIP;
    private String remoteDebuggingCmd;

    /**
     * 使用默认配置文件加载配置
     *
     * @return 配置对象
     */
    public static RpaConfig build() {
        return build(DEFAULT_CONFIG);
    }

    /**
     * 创建配置对象
     *
     * @param config 配置文件地址
     * @return 配置对象
     */
    public static RpaConfig build(String config) {
        return YamlKits.load(config, RpaConfig.class);
    }

    /**
     * 默认配置文件
     */
    private static final String DEFAULT_CONFIG = "rpa.yaml";

    /**
     * 默认配置：selenium 默认驱动
     */
    private static final String DEFAULT_DRIVER = "driver/chromedriver109.exe";
    /**
     * 默认配置：CHROME 启动路径
     */
    private static final String DEFAULT_CHROME_BIN = null;
    /**
     * 默认配置：是否开启代理
     */
    private static final boolean DEFAULT_ENABLE_PROXY = true;
    /**
     * 默认配置：代理端口
     */
    private static final int DEFAULT_PROXY_PORT = 9099;
    /**
     * 默认配置：初始化自动清理 Cookies
     */
    private static final boolean DEFAULT_CLEAN_COOKIES = true;
    /**
     * 默认配置：浏览器全屏展示（F11）
     */
    private static final boolean DEFAULT_FULL_SCREEN = false;
    /**
     * 默认配置：交互时，默认滚动到元素
     */
    private static final boolean DEFAULT_AUTO_SCROLL = true;
    /**
     * 默认配置：构造对象是自动创建自动化浏览器实例
     */
    private static final boolean DEFAULT_AUTO_CREATE = false;
    /**
     * 默认配置：模拟鼠标操作
     */
    private static final boolean DEFAULT_SIMULATE = true;

    @Data
    @ToString
    @NoArgsConstructor
    public static class Selenium {

        /**
         * Selenium 驱动地址
         */
        private String driver = DEFAULT_DRIVER;

        public File getExe() {
            try {
//                var exe = new File(ClassLoader.getSystemResource(driver).toURI());
                var exe = new File(driver);
                if (!exe.exists() || !exe.isFile()) {
                    throw new IllegalArgumentException("Selenium 驱动不可用！");
                }
                return exe;
            } catch (Exception ex) {
                throw new IllegalArgumentException("Selenium 驱动不可用！");
            }
        }
    }

    @Data
    @ToString
    @NoArgsConstructor
    public static class Browser {
        /**
         * Chrome 启动路径
         */
        private String chrome = DEFAULT_CHROME_BIN;
    }

    @Data
    @ToString
    @NoArgsConstructor
    public static class Rpa {

        private Proxy proxy;

        private Cookies cookies;

        /**
         * Selenium 创建时，是否全屏浏览器
         */
        private boolean fullScreen = DEFAULT_FULL_SCREEN;

        /**
         * Selenium 交互元素时，是否自动滚动到元素可见位置
         */
        private boolean autoScroll = DEFAULT_AUTO_SCROLL;

        /**
         * 构造 RPA 对象时，是否自动创建 Selenium 对象
         */
        private boolean autoCreate = DEFAULT_AUTO_CREATE;

        /**
         * 是否启用基于 Windows API 的模拟交互操作
         */
        private boolean simulate = DEFAULT_SIMULATE;
    }


    @Data
    @ToString
    @NoArgsConstructor
    public static class Proxy {
        /**
         * 是否开启网络代理
         */
        private boolean enable = DEFAULT_ENABLE_PROXY;

        /**
         * 网络代理端口
         */
        private int port = DEFAULT_PROXY_PORT;
    }

    @Data
    @ToString
    @NoArgsConstructor
    public static class Cookies {
        /**
         * 构造 Selenium 对象时，自动清理 cookies
         */
        private boolean clean = DEFAULT_CLEAN_COOKIES;
    }


}
